package cz.boxdesign.library.entity.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.stereotype.Component;

import cz.boxdesign.library.entity.Address;
import cz.boxdesign.library.entity.Publisher;
import cz.boxdesign.library.entity.dao.query.NakladatelstviSqlQuery;

/**
 *
 * @author jlattenberk
 *
 */
@Component(value = "nakladatelstviDao")
public class PublisherDaoImpl extends EntityDaoAbstract<Publisher, NakladatelstviSqlQuery> {

  /**
   * @param result
   * @param rs
   * @throws SQLException
   */
  @Override
  void getEntityList(List<Publisher> result, ResultSet rs) throws SQLException {
    while (rs.next()) {
      Publisher n = new Publisher();
      n.setId(rs.getInt("ID"));
      n.setNazev(rs.getString("NAZEV_N"));
      Address a = new Address(rs.getString("ULICE_N"), rs.getString("MESTO_N"), rs.getString("PSC_N"));
      n.setAddress(a);
      result.add(n);
    }
  }

  /**
   * @param entity
   * @param clause
   * @param ps
   * @throws SQLException
   */
  @Override
  void fillStatement(Publisher entity, int queryValue, PreparedStatement ps) throws SQLException {
    NakladatelstviSqlQuery clause = NakladatelstviSqlQuery.getByValue(queryValue);
    switch (clause) {
      case UPDATE: {
        ps.setInt(5, entity.getId());
      }
      case INSERT: {
        ps.setString(1, entity.getNazev());
        ps.setString(2, entity.getAddress().getUlice());
        ps.setString(3, entity.getAddress().getMesto());
        ps.setString(4, entity.getAddress().getPsc());
        break;
      }
      case DELETE: {
        ps.setInt(1, entity.getId());
        break;
      }
      default:
        throw new IllegalArgumentException();
    }
  }

  @Override
  String getSqlQuery(int queryValue) {
    return NakladatelstviSqlQuery.getByValue(queryValue).getQuery();
  }

}
